using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entites;

namespace DAL
{
   public class TinhTrangDAL
    {
        
            private SqlConnection cn = null;
        public TinhTrangDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
       public void Insert (TinhTrang tt )
        {
            SqlCommand cm = new SqlCommand("TinhTrang_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
           
           cm.Parameters.AddWithValue("@TenTinhTrang",tt.tentinhtrang);
            cm.ExecuteNonQuery();
        }
        public void Update(TinhTrang tt)
        {
            SqlCommand cm = new SqlCommand("TinhTrang_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaTinhTrang", tt.matinhtrang);
            cm.Parameters.AddWithValue("@TenTinhTrang",tt.tentinhtrang);
           
          cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("TinhTrang_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaTinhTrang", iD);
            cm.ExecuteNonQuery();
        }

        public tbTinhTrangCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("TinhTrang_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbTinhTrangCollection ttCollection = new tbTinhTrangCollection();

            while (dr.Read())
            {
                TinhTrang tinhtrang = new TinhTrang();
                tinhtrang.matinhtrang = int.Parse(dr.GetValue(0).ToString());
                tinhtrang.tentinhtrang = dr.GetValue(1).ToString();

                ttCollection.Add(tinhtrang);
             }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return ttCollection;
        }
    }
    

}
